/*
 * Copyright (c) Mirth Corporation. All rights reserved.
 * 
 * http://www.mirthcorp.com
 * 
 * The software in this package is published under the terms of the MPL license a copy of which has
 * been included with this distribution in the LICENSE.txt file.
 */

package com.mirth.connect.connectors.jms;

import com.mirth.connect.client.ui.components.MirthFieldConstraints;
import com.mirth.connect.client.ui.panels.connectors.ConnectorSettingsPanel;
import com.mirth.connect.donkey.model.channel.ConnectorProperties;

public class JmsListener extends ConnectorSettingsPanel {
    public JmsListener() {
        initComponents();
        jmsConnectorPanel.init(JmsConnectorPanel.TYPE_LISTENER, getConnectorName());
        reconnectIntervalField.setDocument(new MirthFieldConstraints(0, false, false, true));
    }

    @Override
    public String getConnectorName() {
        return new JmsReceiverProperties().getName();
    }

    @Override
    public ConnectorProperties getProperties() {
        JmsReceiverProperties properties = (JmsReceiverProperties) jmsConnectorPanel.getProperties();
        properties.setSelector(selectorField.getText());
        properties.setReconnectIntervalMillis(reconnectIntervalField.getText());
        return properties;
    }

    @Override
    public void setProperties(ConnectorProperties properties) {
        jmsConnectorPanel.setProperties(properties);
        JmsReceiverProperties jmsReceiverProperties = (JmsReceiverProperties) properties;
        selectorField.setText(jmsReceiverProperties.getSelector());
        reconnectIntervalField.setText(jmsReceiverProperties.getReconnectIntervalMillis());
    }

    @Override
    public ConnectorProperties getDefaults() {
        return new JmsReceiverProperties();
    }

    @Override
    public boolean checkProperties(ConnectorProperties properties, boolean highlight) {
        return jmsConnectorPanel.checkProperties(properties, highlight);
    }

    @Override
    public void resetInvalidProperties() {
        jmsConnectorPanel.resetInvalidProperties();
    }

    // @formatter:off
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        selectorLabel = new javax.swing.JLabel();
        selectorField = new com.mirth.connect.client.ui.components.MirthTextField();
        jmsConnectorPanel = new com.mirth.connect.connectors.jms.JmsConnectorPanel();
        reconnectIntervalField = new com.mirth.connect.client.ui.components.MirthTextField();
        reconnectIntervalLabel = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));

        selectorLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        selectorLabel.setText("Selector:");

        selectorField.setToolTipText("<html>Enter a selector expression to select specific messages from the queue/topic.<br/>Leave blank to read all messages.</html>");

        reconnectIntervalField.setToolTipText("<html>The number of milliseconds between reconnect attempts when a connection error occurs.</html>");

        reconnectIntervalLabel.setText("Reconnect Interval (ms):");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jmsConnectorPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(reconnectIntervalLabel)
                    .addComponent(selectorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(selectorField, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(reconnectIntervalField, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jmsConnectorPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(reconnectIntervalField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(reconnectIntervalLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(selectorField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(selectorLabel)))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.mirth.connect.connectors.jms.JmsConnectorPanel jmsConnectorPanel;
    private com.mirth.connect.client.ui.components.MirthTextField reconnectIntervalField;
    private javax.swing.JLabel reconnectIntervalLabel;
    private com.mirth.connect.client.ui.components.MirthTextField selectorField;
    private javax.swing.JLabel selectorLabel;
    // End of variables declaration//GEN-END:variables
    // @formatter:on
}
